package com.fivefu.core.camera.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.camera.services.RegionService;
import com.fivefu.core.module.city.entity.TCityRegion;
import com.fivefu.core.module.city.entity.TCityStreetTown;
import com.fivefu.core.module.city.service.ITCityRegionService;
import com.fivefu.core.module.city.service.ITCityStreetTownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private ITCityRegionService itCityRegionService;
    @Autowired
    private ITCityStreetTownService itCityStreetTownService;

    private List<TCityRegion> regionList = new ArrayList<>();
    private List<TCityStreetTown> streetList = new ArrayList<>();

    @PostConstruct
    public void queryRegionList(){
        this.regionList = itCityRegionService.list();
        this.streetList = itCityStreetTownService.list();
    }

    /**
     * 通过城区编号查询城区信息
     * @param regionCode 城区编号
     * @return
     */
    @Override
    public TCityRegion queryAreaByCode(String regionCode){
        QueryWrapper<TCityRegion> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_code",regionCode);
        TCityRegion region = itCityRegionService.getOne(wrapper);
        return region;
    }

    /**
     * 通过城区编号查询城区名称
     * @param regionCode 城区编号
     * @return
     */
    @Override
    public String queryAreaNameByCode(String regionCode){
        TCityRegion region = queryAreaByCode(regionCode);
        if(region != null) {
            return region.getRegionName();
        }
        return null;
    }

    /**
     * 通过城区和街道编号查询街道信息
     * @param regionCode 城区编号
     * @param streetCode 街道编号
     * @return
     */
    @Override
    public TCityStreetTown queryStreetByCode(String regionCode, String streetCode){
        QueryWrapper<TCityStreetTown> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("region_code",regionCode);
        wrapper.eq("st_code", streetCode);
        TCityStreetTown street = itCityStreetTownService.getOne(wrapper);
        return street;
    }

    /**
     * 通过城区和街道编号查询街道名称
     * @param regionCode 城区编号
     * @param streetCode 街道编号
     * @return
     */
    @Override
    public String queryStreetNameByCode(String regionCode, String streetCode){
        TCityStreetTown street = queryStreetByCode(regionCode, streetCode);
        if (street != null){
            return street.getStName();
        }
        return null;
    }

    /**
     * 查询所有城区的名称和属地代码
     * @return
     */
    @Override
    public List<Map<String,String>> queryAreaList() {
        QueryWrapper<TCityRegion> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.select("city_code","region_name","region_code");

        List<TCityRegion> list = itCityRegionService.list(wrapper);

        List<Map<String, String>> collect = new ArrayList<>();
        if (list != null){
            collect = list.stream().map(city -> {
                Map<String, String> map = new HashMap<>();
                map.put("regionCode", city.getRegionCode());
                map.put("regionName", city.getRegionName());
                return map;
            }).collect(Collectors.toList());
        }
        return collect;
    }

    /**
     * 通过城区编号查询其他的所有街道名称和街道编号
     * @param regionCode 城区编号
     * @return
     */
    @Override
    public List<Map<String,String>> queryStreetListByRegionCode(String regionCode) {
        QueryWrapper<TCityStreetTown> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StrUtils.isNotEmpty(regionCode)){
            wrapper.eq("region_code",regionCode);
        }
        List<TCityStreetTown> streetTownList = itCityStreetTownService.list(wrapper);

        List<Map<String, String>> collect = new ArrayList<>();
        if (streetTownList != null){
            collect = streetTownList.stream().map(streetTown -> {
                Map<String, String> map = new HashMap<>();
                map.put("stCode", streetTown.getStCode());
                map.put("stName", streetTown.getStName());
                return map;
            }).collect(Collectors.toList());
        }
        return collect;
    }
}
